package com.gt.aep.web.systemManage;

import com.alibaba.fastjson.JSONObject;
import com.gt.aep.base.entity.systemManage.SysIdentityRole;
import com.gt.aep.base.entity.systemManage.SysIdentityRoleUser;
import com.gt.aep.base.entity.systemManage.SysUser;
import com.gt.aep.base.entity.util.Result;
import com.gt.aep.base.service.systemManage.SysIdentityRoleUserService;
import com.gt.aep.base.util.EncryptUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("sysIdentityRoleUser")
public class SysIdentityRoleUserController {

    @Autowired
    SysIdentityRoleUserService sysIdentityRoleUserService;

    // 新增用户时新增--sysUserController

    //删除 --
    // 1、在删除用户时实现
    // 2、删除身份角色时实现

    /**
     * 修改身份角色-用户信息
     */
    @RequestMapping("updateRoleUser")
    public Map<String, Object> updateIdentityRoleUser(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String userId = (String) dataObject.get("userId");
        String[] roleIds = (String[]) dataObject.get("roleIds");
        //修改思路：
        // 1、根据删除所有用户ID为 userId 的记录
        // 2、新增角色-用户关系记录
        int i = sysIdentityRoleUserService.deleteByUid(userId);
        if (i>0){
            int l = 0;
            for(int j = 0; j < roleIds.length; j++){
                SysIdentityRoleUser sysIdentityRoleUser = new SysIdentityRoleUser();
                sysIdentityRoleUser.setId(UUID.randomUUID().toString());
                sysIdentityRoleUser.setIdentityRoleId(roleIds[j]);
                sysIdentityRoleUser.setUid(userId);
                int k = sysIdentityRoleUserService.insert(sysIdentityRoleUser);
                if (k>0){
                    l++;
                }
            }
            if (l == roleIds.length){
                return Result.success("修改成功");
            }else{
                return Result.error("修改失败");
            }
        }else{
            return Result.error("清除旧关系失败");
        }
    }

    /**
     * 根据用户ID获取角色列表
     * @return
     */
    @RequestMapping("getRoleListByUid")
    public Map<String, Object> getRoleListByUid(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String userId = (String) dataObject.get("userId");
        List<SysIdentityRole> sysIdentityRoleList = sysIdentityRoleUserService.getListByUid(userId);
        int count = sysIdentityRoleUserService.getCountByUid(userId);
        List<Map<String, Object>> dataList = new LinkedList<>();
        Map<String, Object> model = new HashedMap();
        model.put("sysRoleList", sysIdentityRoleList);
        model.put("count",count);
        dataList.add(model);
        return EncryptUtil.encryptData(dataList, jsPublicKey);
    }


    /**
     * 根据身份角色ID获取用户列表
     * @return
     */
    @RequestMapping("getListByIdentityRoleId")
    public Map<String, Object> getListByIdentityRoleId(String data, String encryptAesKey){
        //解密前端发送过来的数据
        String jsPublicKey = null;
        Map<String, Object> decData = EncryptUtil.decode(data,encryptAesKey);
        String jsAesKey = (String) decData.get("jsAesKey");
        String decrypt = (String) decData.get("data");
        //将String类型数据转化为对象
        JSONObject dataObject = new JSONObject(JSONObject.parseObject(decrypt));
        //根据前端的字段获取相对应的属性
        jsPublicKey = (String) dataObject.get("jsPublicKey");
        String identityRoleId = (String) dataObject.get("identityRoleId");
        List<SysUser> sysUserList = sysIdentityRoleUserService.getListByIdentityRoleId(identityRoleId);
        int count = sysIdentityRoleUserService.getCountByIdentityRoleId(identityRoleId);
        List<Map<String, Object>> dataList = new LinkedList<>();
        Map<String, Object> model = new HashedMap();
        model.put("sysUserList", sysUserList);
        model.put("count",count);
        dataList.add(model);
        return EncryptUtil.encryptData(dataList, jsPublicKey);
    }
}
